//simplied from DecoProject's Deco3dUITool
//chenxz July29th,2007

//#include "DecoRoomWare.h"
#include "stdafx.h"
#include ".\deco3duitool.h"

#include "RenderType.h"
#include "Scene.h"
#include "Camera.h"
#include "SceneObj.h"
//#include "DecoObjectFactory.h"
#include "RenderMiscs.h"
#include "tetVisualizer.h"
//#include "RoomAndDoor.h"
//#include "furniture.h"
//#ifdef DECO_APP
//#include "../app_src/Resource.h"
//#else
//#include "../activeX_src/Resource.h"
//#endif

#pragma warning(push,3)
#pragma warning(disable: 4482)
//////////////////////////////////////////////////////////////////////////

////helper function
//static DecoSceneObject* selectWallOrFloor(FLOAT x, FLOAT y, vector3& interp)
//{
//	vector3 origin, dir;
//	scene->PointScreen2WorldRay(vector2(x, y), origin,  dir);
//	return (scene->GetRoomByName("")->SelectWallOrFloor(origin, dir, interp));
//}
//
//static std::string attachTexToObject(const std::string& texpath, DecoSceneObject* obj)
//{
//	std::string prevTex = "";
//	if(obj->GetType() == WallT)
//	{
//		DecoWall* pWall = static_cast<DecoWall* >(obj);
//		prevTex = pWall->GetTexture();
//		if(texpath == "")
//			pWall->ClearTexture();
//		else
//			pWall->ApplyTexture(texpath, DEFAULT_TEX_WIDTH, DEFAULT_TEX_HEIGHT);
//	}
//	else if(obj->GetType() == FloorT)
//	{
//		DecoFloor* pFloor = static_cast<DecoFloor* >(obj);
//		prevTex = pFloor->GetTexture();
//		if(texpath == "")
//			pFloor->ClearTexture();
//		else
//			pFloor->ApplyTexture(texpath, DEFAULT_TEX_WIDTH, DEFAULT_TEX_HEIGHT);
//	}
//	else if(obj->GetType() == WallT)
//	{
//		DecoWindow* pWin = static_cast<DecoWindow* >(obj);
//		prevTex = pWin->GetTexture();
//		if(texpath == "")
//			pWin->ClearTexture();
//		else
//			pWin->ApplyTexture(texpath);
//	}
//	//else if(obj->GetType() == DoorT)
//	//{
//	//	DecoDoor* pDoor = static_cast<DecoDoor* >(obj);
//	//	prevTex = pDoor->GetTexture();
//	//	pDoor->ApplyTexture(texpath);
//	//}
//	else
//		ASSERT(FALSE);
//	return prevTex;
//}
////////////////////////////////////////////////////////////////////////////
//
//
//DecoSceneObject* Deco3dUITool::newlyAddedObj = NULL;
const static FLOAT ROTATE_RATIO = 0.025f;

Deco3dUITool::Deco3dUITool(void) : numberButton(0)
{
}

Deco3dUITool::~Deco3dUITool(void)
{
}

void Deco3dUITool::init(DecoScene* scn)
{
	scene = scn;
	isShiftDown = FALSE;
	scene->DeselectAll();
	uimode = UIMode::ptrT;
	mode = nullT;
	scene = scn;
	//newlyAddedObj = NULL;
	//newlyAddedTexPath = "";
	//prevInsideWall = NULL;
	//prevTexPath = "";
}

void Deco3dUITool::exit()
{}

//void Deco3dUITool::NotifyDelNewlyAdded(DecoSceneObject* obj)
//{
//	if(newlyAddedObj == obj)
//		newlyAddedObj = NULL;
//}

void Deco3dUITool::SetUIMode(UIMode md)
{
	if(md >= UIMode::ptrT && md <= UIMode::measureT)
		uimode = md;
	UpdateMiscStuff();	
	//if(md == measureT)
	//	measureMode = MeasureMode::measureNullT;

	//if(md == ptrT)
	//	::SetCursor(NULL);
	//else if(md == panT)
	//	::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_HAND));
	//else if(md == rotateT)
	//	::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_ROTATE));
	//else if(md == resizeT)
	//	::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_RESIZE));
	//else 
	//	::SetCursor(NULL);
}

Deco3dUITool::UIMode Deco3dUITool::GetUIMode()const
{
	return uimode;
}

BOOL Deco3dUITool::onLButtonDown(FLOAT x, FLOAT y)
{
	DecoScene* pscn = scene;

	lastX = x;
	lastY = y;

	lastMouseDownX = x;
	lastMouseDownY = y;

	//handle newly added stuff at very first
	//if(newlyAddedTexPath != "")
	//{
	//	newlyAddedTexPath = "";
	//	prevAttachedObj = NULL;
	//	prevTexPath = "";
	//	pscn->DeselectAll();
	//	return TRUE;
	//}
	//if(newlyAddedObj != NULL)
	//{
	//	newlyAddedObj = NULL;
	//	pscn->DeselectAll();
	//	return TRUE;
	//}

	DecoSceneObject* obj = pscn->SelectObj(x, y);
	assert(pscn);
/*	if (uimode == UIMode::ptrT)
	{
		if (pscn->IsSomethingSelected())
		{
			if (!pscn->MouseClickOnBlank((FLOAT)x, (FLOAT)y))
			{
				pscn->DeselectAll();
				return TRUE;
			}
		}
		else
			obj = pscn->SelectObj((FLOAT)x, (FLOAT)y);
	}
	else */if (uimode == UIMode::rotateT || uimode == UIMode::panT || uimode == UIMode::resizeT)
	{
		//obj = pscn->MouseOnSelectedObj((FLOAT)x, (FLOAT)y);
	}
	//else if (uimode == UIMode::measureT && measureMode == MeasureMode::measureNullT)
	//{
	//	DecoMeasure::GetSingleton()->ClearPoints();
	//	vector3 intersectPoint;
	//	if (pscn->SelectMeasurePoint(x, y, &intersectPoint))
	//	{
	//	DecoMeasure::GetSingleton()->SetPoint1(intersectPoint);
	//	measureMode = MeasureMode::measureMoveT;
	//	}
	//	UpdateMiscStuff();
	//	return TRUE;
	//}
	//else if (uimode == UIMode::measureT && measureMode == MeasureMode::measureMoveT)
	//{
	//	measureMode = MeasureMode::measureNullT;
	//	UpdateMiscStuff();
	//	return TRUE;
	//	//OutputDebugString("DrawLine");
	//}

	//if (pscn->IsSomethingSelected())
	//{
	//	obj = pscn->MouseClickOnBlank((FLOAT)x, (FLOAT)y);
	//}
	//else
	//	obj = pscn->SelectObj((FLOAT)x, (FLOAT)y);
	if(obj != NULL)
	{
		/*focusObj = obj;
		scene->selectedObjs.clear();
		scene->selectedObjs.push_back(obj);*/
		if(uimode == UIMode::panT && obj->IsMovable())
		{
			mode = InternalMode::moveT;
		}
		else if(uimode == UIMode::rotateT && obj->IsRotatable())
		{
			mode = InternalMode::rotT;
		}
	/*	else if(uimode == UIMode::resizeT && 
			((obj->GetType() == DoorT) || obj->GetType() == WindowT))
		{
			mode = InternalMode::reszT;
			vector3 ext = obj->GetRenderData()->GetBoundingBox().GetExtent();
			reszX = ext.x;
			reszY = ext.y;
		}*/
		//else if (uimode == UIMode::measureT && measureMode == MeasureMode::measureNullT)
		//{
		//	DecoMeasure::GetSingleton()->ClearPoints();
		//	vector3 intersectPoint;
		//	if (pscn->SelectMeasurePoint(x, y, &intersectPoint))
		//	{
		//		DecoMeasure::GetSingleton()->SetPoint1(intersectPoint);
		//		measureMode = MeasureMode::measureMoveT;
		//	}
		//}
		//else if (uimode == UIMode::measureT && measureMode == MeasureMode::measureMoveT)
		//{
		//	measureMode = MeasureMode::measureNullT;
		//	//OutputDebugString("DrawLine");
		//}
		else
		{
			mode = InternalMode::orbitT;
		}
		UpdateMiscStuff();
		return TRUE;
	}
	else
	{
		//assert(false);
		mode = InternalMode::orbitT;
	}
	return FALSE;//no need to update
}

void Deco3dUITool::UpdateMiscStuff()
{
	DecoSceneObject* focusObj = scene->GetFocusObject();
	if(focusObj != NULL)
	{
		if(uimode == UIMode::panT && focusObj->IsMovable())
		{
			DecoRenderMisc::GetSingleton()->ShowRotation(FALSE);
			DecoRenderMisc::GetSingleton()->ShowBox(TRUE);
			DecoRenderMisc::GetSingleton()->ShowAxis(TRUE);

		}
		else if(uimode == UIMode::rotateT && focusObj->IsRotatable())
		{
			DecoRenderMisc::GetSingleton()->ShowRotation(TRUE);
			DecoRenderMisc::GetSingleton()->ShowBox(TRUE);
			DecoRenderMisc::GetSingleton()->ShowAxis(FALSE);
		}
		else
		{
			DecoRenderMisc::GetSingleton()->ShowRotation(FALSE);
			DecoRenderMisc::GetSingleton()->ShowBox(TRUE);
			DecoRenderMisc::GetSingleton()->ShowAxis(FALSE);
		}
	}

	//DecoRenderMisc::GetSingleton()->ShowGrid((UIMode::measureT == uimode));
	//if (UIMode::measureT != uimode)
	//{		
	//	DecoMeasure::GetSingleton()->ClearPoints();
	//}
}

BOOL Deco3dUITool::onRButtonDown(FLOAT x, FLOAT y)
{
	//if(uimode == )
	mode = InternalMode::orbitT;
	lastX = x;
	lastY = y;

	lastMouseDownX = x;
	lastMouseDownY = y;

	//if (uimode == UIMode::measureT && measureMode == MeasureMode::measureMoveT)
	//{		
	//	DecoMeasure::GetSingleton()->ClearPoints();
	//	measureMode = MeasureMode::measureNullT;
	//	return TRUE;
	//}
	return FALSE;
}

BOOL Deco3dUITool::onLButtonUp(FLOAT x, FLOAT y)
{
	//if(newlyAddedObj != NULL)
	//{
	//	newlyAddedObj = NULL;
	//	return FALSE;
	//}
	const static int MOUSE_CLICK_SELECT_THRESHOLD = 10;

	DecoScene* pscn = scene;
	//DecoSceneObject* focusObj = pscn->GetFocusObject();
	if (/*uimode == ptrT && */
		uimode != measureT && 
		vector2(x - lastMouseDownX, y - lastMouseDownY).lengthSqr() 
		< MOUSE_CLICK_SELECT_THRESHOLD)
	{
//		if (pscn->IsSomethingSelected())
//		{
//			if (!pscn->MouseClickOnBlank((FLOAT)x, (FLOAT)y) )
////				|| pscn->GetFocusObject() == prevInsideWall)
//			{
//				pscn->DeselectAll();
//				//focusObj = NULL;
//				UpdateMiscStuff();
//				SetUIMode(ptrT);
//				//return TRUE;
//			}
//		}
//		else
//		{
			DecoSceneObject* focusObj = pscn->SelectObj((FLOAT)x, (FLOAT)y);
			if(focusObj != NULL)
			{
				pscn->SetFocusObject(focusObj);
				UpdateMiscStuff();
			}
			//return TRUE;
		//}
	}
	
	//back to ptr mode///////
	//if(uimode != measureT)
 //       SetUIMode(ptrT);
	/////////////////////////

	mode = InternalMode::nullT;
	return TRUE;
}

BOOL Deco3dUITool::onRButtonUp(FLOAT x, FLOAT y)
{
//	const static int MOUSE_CLICK_SELECT_THRESHOLD = 10;

//	DecoScene* pscn = scene;
	//DecoSceneObject* focusObj = pscn->GetFocusObject();
	//if (vector2(x - lastMouseDownX, y - lastMouseDownY).lengthSqr() 
	//	< MOUSE_CLICK_SELECT_THRESHOLD)
	//{
	//	if(newlyAddedObj != NULL)
	//	{
	//		DecoObjectFactory::Destroy(newlyAddedObj);
	//		init();
	//		return TRUE;
	//	}
	//	else if(newlyAddedTexPath != "")
	//	{
	//		if(prevAttachedObj != NULL)//restore the previous obj with its textures
	//		{
	//			//clean downloading related with current tex
	//			DownloadMgr::getSingleton()->removeDownloadBack(prevAttachedObj);
	//			attachTexToObject(prevTexPath, prevAttachedObj);
	//		}
	//		init();
	//		return TRUE;
	//	}
	//}
	mode = InternalMode::nullT;
	return FALSE;
}


BOOL Deco3dUITool::onMouseMove(FLOAT x, FLOAT y)
{
	DecoSceneObject* focusObj = scene->GetFocusObject();
	if(mode == InternalMode::orbitT)
	{
		DecoCamera& g_cm = *(scene->camera);

		if (!g_cm.isOrthogonal())
		{		
			float stackrad = (y - lastY) / 10.f;
			float slicerad = (x - lastX) / 10.f;

			g_cm.localRotate(stackrad, AXIS_X);
			g_cm.globalRotate(-slicerad, AXIS_Y);
			lastX = x;
			lastY = y;
		}

		return TRUE;
	}
	//else if(newlyAddedTexPath != "")
	//{
	//	vector3 interp;
	//	DecoSceneObject* interObj = selectWallOrFloor(x,y,interp);
	//	if(interObj != NULL && 
	//		(interObj->GetType() == FloorT || interObj->GetType() == WallT 
	//		|| interObj->GetType() == WindowT || interObj->GetType() == DoorT)
	//		)
	//	{
	//		if(interObj != prevAttachedObj)
	//		{
	//			if(prevAttachedObj != NULL)//restore the previous obj with its textures
	//			{
	//				//clean downloading related with current tex
	//				DownloadMgr::getSingleton()->removeDownloadBack(prevAttachedObj);
	//				attachTexToObject(prevTexPath, prevAttachedObj);
	//			}
	//			prevAttachedObj = interObj;
	//			prevTexPath = attachTexToObject(newlyAddedTexPath,interObj);
	//			return TRUE;
	//		}
	//	}
	//}
	//else if(newlyAddedObj != NULL)
	//	//prompt the user to select the position to put the obj
	//{
	//	vector3 interp;
	//	DecoSceneObject* interObj = selectWallOrFloor(x,y,interp);
	//	if(interObj != NULL)
	//	{
	//		if(newlyAddedObj->GetType() == FurnitureT && interObj->GetType() == FloorT)
	//		{
	//			DecoFurniture* pFur = static_cast<DecoFurniture* >(newlyAddedObj);
	//			pFur->SetPosition(interp);		
	//			pFur->Move(vector3(0, pFur->GetRenderData()->GetBoundingBox().GetExtent().y, 0));
	//			return TRUE;
	//		}					
	//		else if(newlyAddedObj->GetType() == WindowT && interObj->GetType() == WallT)
	//		{
	//			DecoWindow* pWin = static_cast<DecoWindow* >(newlyAddedObj);
	//			DecoWall* parentW = static_cast<DecoWall*>(interObj);
	//			pWin->FixToWall(parentW);
	//			vector3 interpOnWall = parentW->GetCoords().ConvertToLocal(interp);
	//			interpOnWall.z = 0;//no movement on z				
	//			pWin->SetPosition(parentW->GetCoords().ConvertToParent(interpOnWall));
	//			return TRUE;
	//		}
	//	}		
	//}
	else if(mode == InternalMode::moveT)
	{
		DecoCamera& g_cm = *(scene->camera);
		//DecoSceneObject* obj =  
		if(signedAbs(x- lastX) + signedAbs(y-lastY) > 2)//distinguish little movements of mouse-single-click
		{
			if(focusObj)
			{
				//Plane testFloorConstraint(vector3(1, 0, 0), vector3(0, 0, 0), vector3(0, 0, 1));
				//focusObj->Move(
				//	scene->vectorScreen2World(vector2(lastX, lastY), vector2(x,y),
				//	(focusObj->GetRenderData()->GetBoundingBox().GetCenter()-g_cm.getEye()).length(), &testFloorConstraint, 1));

				//if(focusObj->GetConstraints().size()==1)
				//{
				//	const DecoConstraint& cnstr = *(focusObj->GetConstraints().begin());
				//	focusObj->Move(
				//		scene->vectorScreen2World(vector2(lastX, lastY), vector2(x,y),
				//		(focusObj->GetRenderData()->GetBoundingBox().GetCenter()-g_cm.getEye()).length(), &(cnstr.getPlane()), 1));
				//}
				//else
				//{
					focusObj->Move(
						scene->vectorScreen2World(vector2(lastX, lastY), vector2(x,y),
						(focusObj->GetRenderData()->GetBoundingBox().GetCenter()-g_cm.getEye()).length(), NULL, 0));
//				}
			}

			lastX = x;
			lastY = y;
		}
		return TRUE;
	}
	else if(mode == InternalMode::rotT)
	{
  		if(signedAbs(x- lastX) + signedAbs(y-lastY) > 2)//distinguish little movements of mouse-single-click
		{
			FLOAT offset2 = 0;
			if(focusObj && focusObj->IsRotatable())
			{
				DecoCamera& g_cm = *(scene->camera);
				FLOAT offset = 0;
				if(g_cm.isOrthogonal())
					offset = signedAbs(x - lastX) > signedAbs(y - lastY) ? x - lastX : y - lastY;  
				else
				{
					offset = x - lastX;
					offset2 = y - lastY;
				}
				vector3 eyePos = g_cm.getEye();
				vector3 objPos = focusObj->GetCoords().origin();
				vector3 ray = eyePos - objPos;
				ray.normalize();
				
				vector3 up(0, 1, 0);
				vector3 right = CrossProduct(up, ray);
				right.normalize();
				vector3 realUp = CrossProduct(ray, right);
				realUp.normalize();
				vector3 rotateAxis = offset * realUp - offset2 * right;
				FLOAT angle = ROTATE_RATIO * sqrt(offset * offset + offset2 * offset2);
   				focusObj->RotateAlongGlobalVector(rotateAxis, angle);
				//focusObj->Rotate(AXIS_Y, ROTATE_RATIO*offset);
	
				
				
				//if(fabs(offset2) > EPSILON)
				//{
 			//		vector3 eyePos = g_cm.getEye();
				//	vector3 objPos = focusObj->GetCoords().origin();
				//	vector3 eyePro(eyePos.x, objPos.y, eyePos.z);
				//	vector3 axisz = eyePro - objPos;
				//	axisz.normalize();
				//	vector3 axisy(0,1,0);
				//	vector3 axisx = CrossProduct(axisz, axisy);
 			//		double rotBase[3][3] = {{axisx.x, axisx.y, axisx.z},
				//	{axisy.x,axisy.y,axisy.z},
				//	{axisz.x,axisz.y,axisz.z}
				//	};
				//	Coords rotFrm(rotBase, objPos);
				//	Coords objFrmInRotFrm = rotFrm.ConvertToLocal(focusObj->GetCoords());
				//	rotFrm.RotateAlongLocalAxis(-ROTATE_RATIO*offset2, AXIS_X);
				//	Coords test = rotFrm.ConvertToParent(objFrmInRotFrm);
				//	focusObj->SetCoords(rotFrm.ConvertToParent(objFrmInRotFrm));
				//}
				lastX = x;
				lastY = y;
				return TRUE;
			}
		}
	}
	else if(mode == InternalMode::reszT)
	{
		//if(focusObj != NULL)
		//{
		//	const static FLOAT RESZ_RATIO = 0.3f;
		//	FLOAT scale_x = (reszX + RESZ_RATIO*(x - lastX))/reszX;
		//	FLOAT scale_y = (reszY + RESZ_RATIO*(y - lastY))/reszY;
		//	focusObj->Scale(vector3(scale_x, scale_y, 1));
		//	//if(offx)
		//}
		//lastX = x;
		//lastY = y;
		//return TRUE;
	}
	/*else if (uimode == UIMode::measureT && measureMode == MeasureMode::measureMoveT)
	{		
		assert(DecoMeasure::GetSingleton()->GetPointNum() >= 1);
		vector3 intersectPoint;
		if (scene->SelectMeasurePoint(x, y, &intersectPoint))
		{
			DecoMeasure::GetSingleton()->SetPoint2(intersectPoint);			
		}
		return TRUE;
	}*/
	return FALSE;
}

BOOL Deco3dUITool::onMouseScroll(FLOAT z)
{
	FLOAT stepSize = z*0.0015f;
	FLOAT magRatio = 5*stepSize;
	if(isShiftDown && scene->GetFocusObject() != NULL)
	{
		//FLOAT sclfac = magRatio > 0 ? magRatio : -1.f/magRatio;
		//scene->GetFocusObject()->Scale(vector3(sclfac,sclfac,sclfac));
	}
	else
	{
		DecoCamera& g_cm = *(scene->camera);
		
		if(g_cm.isOrthogonal())
		{
			g_cm.magnify(magRatio > 0 ? magRatio : -1.f/magRatio);
		}
		else
			g_cm.walkFront(stepSize);
//	scene->UpdateWallHide();
	}

	return TRUE;
}

const static INT SHIFTTOINT = 48;
const static INT CTRLTOINT = 49;
const static INT SLASHTOINT = 2;
const static INT SPACETOINT = 64;
const static INT ESCTOINT = 59;
const static INT ONEPOINT = 81;
const static INT TWOPOINT = 82;
const static INT THREEPOINT = 83;

BOOL Deco3dUITool::onKeyUp(char key)
{
	switch(key)
	{
	case SHIFTTOINT:
		isShiftDown = FALSE;
		break;
	case CTRLTOINT:
		if (UIMode::measureT != uimode)
			//uimode = UIMode::ptrT;
			SetUIMode(ptrT);
		break;
	case SLASHTOINT:case SPACETOINT:
		if (UIMode::measureT != uimode)
			//uimode = UIMode::ptrT;
			SetUIMode(ptrT);
		break;
	case 'y':
		uimode = UIMode::ptrT;
		break;
	case ESCTOINT:
		init(scene);
		break;
	case ONEPOINT: 
		numberButton = 0;
		break;
	case TWOPOINT: 		
		numberButton = 0;
		break;
	case THREEPOINT:
		numberButton = 0;
		break;
	}

	UpdateMiscStuff();
	return TRUE;
}

BOOL Deco3dUITool::onKeyDown(char key)
{
	DecoCamera& g_cm = *(scene->camera);
	const FLOAT stepSize = 0.05f;
	const FLOAT magRatio = 0.8f;
	vector3 extend = scene->GetSceneBox().GetExtent() * 2;
	INT x = 0, y = 0, width = 0, height = 0;
	scene->RI->GetViewport(x, y, width, height);
	FLOAT aspRatio = static_cast<FLOAT>(width) / height;
	//switch(key)
	//{
	//	case 'w':case 's': case 'a': case 'd':
	//	{
	//		DecoWall* pWall = scene->GetRoomByName("")->GetOwnerWall(
	//			scene->camera->getEye());
	//		if(prevInsideWall != pWall && prevInsideWall != NULL)
	//		{
	//			prevInsideWall->DynamicChangeFlag(OpSelectable, TRUE);
	//		}
	//		if(pWall != NULL)
	//		{
	//			prevInsideWall = pWall;
	//			pWall->DynamicChangeFlag(OpSelectable, FALSE);
	//		}
	//		//scene->UpdateWallHide();
	//	}
	//	break;
	//}
	switch(key)
	{
	case SHIFTTOINT:
		isShiftDown = TRUE;
		break;
	case 'w': 
		if(isShiftDown)
		{
			if (g_cm.isOrthogonal())
				g_cm.magnify(magRatio);
			else
				g_cm.slide(0, stepSize, 0);
		}
		else
		{
			vector3 base, b0, b1;
			g_cm.getCoords().GetBase(b0, b1, base);
			g_cm.slide(-stepSize * base.x, -stepSize * base.y, -stepSize * base.z);
		}
		break;
	case 's':
		if(isShiftDown)
		{
			if (g_cm.isOrthogonal())
				g_cm.magnify(1 / magRatio);
			else
				g_cm.slide(0, -stepSize, 0);
		}
		else
		{
			vector3 base, b0, b1;
			g_cm.getCoords().GetBase(b0, b1, base);
			g_cm.slide(stepSize * base.x, stepSize * base.y, stepSize * base.z);
		}
		break;
	case 'a': 
		g_cm.walkLeft(stepSize);
		break;
	case 'd':
		g_cm.walkLeft(-stepSize);
		break;
	case 'e':
		g_cm.slide(0, stepSize, 0);
		break;
	case 'q':
		g_cm.slide(0, -stepSize, 0);
		break;
	//case 'p':
	//	uimode = (UIMode::measureT == uimode) ? UIMode::ptrT : UIMode::measureT;		
	//	measureMode = MeasureMode::measureNullT;
	//	break;
	case 't':
		scene->ToggleShowGrid();
		break;
	case CTRLTOINT:
		if (UIMode::measureT != uimode)
			//uimode = UIMode::panT;
			SetUIMode(panT);
		break;
	case SLASHTOINT:case SPACETOINT:
		if (UIMode::measureT != uimode)
			SetUIMode(rotateT);
			//uimode = UIMode::rotateT;
		break;

//#ifdef MFC_ENABLED
//	case 'z':
//		//scene->LockAll();
//		scene->SaveToXML("zsave.xml");
//		break;
//#endif
	case 'z':
		g_cm.setOrtho(static_cast<FLOAT>(extend.x * 2), static_cast<FLOAT>(2 * extend.x / aspRatio), static_cast<FLOAT>(DEFAULT_NEAR_PLANE), DEFAULT_FAR_PLANE);
		g_cm.lookFromSide(AXIS_Z, isShiftDown);
		break;
	case 'x':
		g_cm.setOrtho(static_cast<FLOAT>(extend.z * 2), static_cast<FLOAT>(2 * extend.z / aspRatio), static_cast<FLOAT>(DEFAULT_NEAR_PLANE), DEFAULT_FAR_PLANE);
		g_cm.lookFromSide(AXIS_X, isShiftDown);
		break;
	case 'y':
		g_cm.setOrtho(static_cast<FLOAT>(extend.y * 2), static_cast<FLOAT>(2 * extend.y / aspRatio), static_cast<FLOAT>(DEFAULT_NEAR_PLANE), DEFAULT_FAR_PLANE);
		g_cm.lookFromSide(AXIS_Y, isShiftDown);
		break;
	case 'p':
		g_cm.setFrustum(45, aspRatio, static_cast<FLOAT>(DEFAULT_NEAR_PLANE), DEFAULT_FAR_PLANE);
		g_cm.set(vector3(0, 0, INIT_CAMERA), vector3(0, 0, 0), vector3(0, 1, 0));
		break;
	case 'N':
		{
			DecoSceneObject* focusObj = scene->GetFocusObject();
			scene->DeselectAll();		
			if(focusObj)
			{
				scene->Destroy(focusObj);
				//if(focusObj->GetType() == WallT)
				//{
				//	DecoWall* pWall = static_cast<DecoWall*>(focusObj);
				//	pWall->ClearTexture();
				//}
				//else if(focusObj->GetType() == FloorT)
				//{
				//	DecoFloor* pFloor = static_cast<DecoFloor* >(focusObj);
				//	pFloor->ClearTexture();
				//}
				///*else if(focusObj->GetType() == WindowT)
				//{
				//DecoWindow* pWin = static_cast<DecoWindow*>(focusObj);
				//pWin->ClearTexture();
				//}*/
				//else if(focusObj->GetType() == FurnitureT || focusObj->GetType() == WindowT)
				//{
				//	DecoObjectFactory::Destroy(focusObj);
				//	focusObj = NULL;
				//}
			}
		}
		break;
	case ONEPOINT:
		numberButton = 1;
		break;
	case TWOPOINT:
		numberButton = 2;
		break;
	case THREEPOINT:
		numberButton = 3;
		break;
	//case 'y':
	//	uimode = UIMode::resizeT;
	//	break;
	default: return FALSE;
	}
	return TRUE;
}

DecoSceneObject* Deco3dUITool::GetFocusedObj()const
{
	return scene->GetFocusObject();
}
//
//void Deco3dUITool::NotifyNewlyAdded(DecoSceneObject* pObj)
//{
//	newlyAddedObj = pObj;
//	scene->SetFocusObject(pObj);
//	UpdateMiscStuff();
//}
//
//void Deco3dUITool::NotifyNewlyAdded(const std::string& texpath)
//{
//	newlyAddedTexPath = texpath;
//	prevTexPath = "";
//	prevAttachedObj = NULL;
//}
//
//DecoSceneObject* Deco3dUITool::GetNewlyAddedObject()const
//{
//	return newlyAddedObj;
//}
//
//const std::string& Deco3dUITool::GetNewlyAddedTexture()const
//{
//	return newlyAddedTexPath;
//}
#pragma warning(default: 4482)

#pragma warning(pop)